<?php

namespace App\Http\Controllers\Posts;

use App\Models\Article;
use App\Models\Cates;
use App\Models\Comment;
use App\Models\Tab;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{
    /** 文章列表 */
    public function index(Request $request)
    {
        $cates_id = $request->input('cates_id');
        $tab = $request->input('tab');
        if($request->isMethod('post')){
//            DB::enableQueryLog();
            $model = Article::with(['user','cates','tabs'])
                ->when(!empty($tab),function($query)use($tab){
                    $query->where('tab',$tab);
                })
                ->when(!empty($cates_id),function($query)use($cates_id){
                    $query->where('cates_id',$cates_id);
                })
                ->orderBy('created_at','desc')
                ->paginate(config('base.limit'));
//            return response()->json(DB::getQueryLog());

            return $this->data($model->items(),$model->total());
        }

        $cates = Cates::where('pid',0)->orderBy('sort','asc')->pluck('name','id');
        $tabs = Tab::pluck('name','id');
        return view('posts.article.index',['cates'=>$cates,'tabs'=>$tabs,'cates_id'=>$cates_id,'tab'=>$tab]);
    }

    /** 编辑文章 */
    public function edit(Request $request)
    {
        $id = $request->get('id');

        if(empty($id)){
            $model = new Article();
        }else{
            $model = Article::find($id);
        }

        if($request->isMethod('post')){
            $data = $request->all();

            /** 基础验证 */
            $validator = $this->validateArticle($data);
            if ($validator->fails()) {
                return $this->fail($validator->errors()->first());
            }

            $user_id = empty($data['user_id'])?(User::randId()):$data['user_id'];

            $model->cates_id    = $data['cates_id'];
            $model->title       = $data['title'];
            $model->content     = $data['content'];
            $model->tab         = $data['tab'];
            $model->user_id     = $user_id;

            $model->save();

            return $this->success('保存成功!');
        }

//        $cates  = Cates::lists($status=false,$structure='plumb','&nbsp;&nbsp;');
//        $cates  = array_column($cates,'name','id');
        $cates = Cates::where('pid',0)->orderBy('sort','asc')->pluck('name','id');
        $tabs   = Tab::pluck('name','id')->toArray();


        return view('posts.article.edit',['model'=>$model,'cates'=>$cates,'tabs'=>$tabs]);
    }

    /** 评论帖子 */
    public function comment(Request $request)
    {
        if($request->isMethod('post')){
            $data = $request->all();

            $model = new Comment();
            $model->article_id  = $data['article_id'];
            $model->content     = $data['content'];
            $model->user_id     = User::randId();

            $model->save();

            Article::where('id',$data['article_id'])->increment('comment_count');
            return $this->success('评论成功!');
        }
        $id = $request->get('id');
        $model = Article::find($id);

        return view('posts.article.comment',['model'=>$model]);
    }

    /** 删除文章 */
    public function del(Request $request)
    {
        $id = $request->get('id');
        Article::where('id',$id)->delete();
        return $this->success('删除成功!');
    }

    /** 验证 */
    public function validateArticle($data)
    {
        $validator = Validator::make($data, [
            'cates_id'  => 'required|exists:cates,id',
            'title'     => 'required|max:255',
            'tab'       => 'required',
            'content'   => 'required'
        ], [
            'required'  => ':attribute不得为空',
            'max'       => ':attribute不得超过:max字符',
            'exists'    => ':attribute不存在',
        ], [
            'cates_id'  => '分类',
            'name'      => '文章标题',
            'tab'       => '标签',
            'content'   => '内容'
        ]);
        return $validator;
    }
}
